/**
 * Created by Administrator on 2015/12/10.
 */
"use strict";

var F = require('../../../common/function');
var _o2oServiceApi = require('../../../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;
var _ = require('underscore');
var MUsers = require('../../../model/users.js');

exports.bind_user = function *(sid, uid) {
  //1.验证要绑定的机构是否有效
  let vendorId = 0;
  let organization_detail = yield o2oServiceApi.post('shop', 'detail', 1, {act: 1, id: sid});

  if (parseInt(organization_detail['result']['error']) == 1 || organization_detail.status == 400) {
    return F.returnMsg(300, organization_detail['message']['msg'], 1);
  } else if (organization_detail['result']['status'] != 1) {
    return F.returnMsg(300, '传入的机构无效，不能绑定', 1);
  } else if (organization_detail['result']['vendorid'] > 0) {
    vendorId = organization_detail['result']['vendorid'];
  }
  if (vendorId == 0) {
    return F.returnMsg(300, '传入的机构还没被机构认证，不能绑定', 1);
  }
  //2.验证商家是否有效
  let vendor_detail = yield o2oServiceApi.post('vendor', 'detail', 1, {act: 1, id: vendorId});
  if (parseInt(vendor_detail['result']['error']) == 1 || vendor_detail.status == 400) {
    return F.returnMsg(300, vendor_detail['message']['msg'], 1);
  } else if (vendor_detail['result']['status'] != 1) {
    return F.returnMsg(300, '你要绑定的机构所属商家无效，不能绑定', 1);
  }
  //3.根据vid将商家下面的店长rid找出来
  let vendor_store_keeper_fromDB = 0;
  let vendorRoleList = yield o2oServiceApi.post('authority', 'roleList', 1, {act: 1, vid: vendorId});

  if (vendorRoleList && vendorRoleList['result'] && vendorRoleList['result'].length > 0) {
    vendor_store_keeper_fromDB = _.filter(vendorRoleList['result'], function (role) {
      return role.name == '店长' && role.status ==1;
    });

    if (vendor_store_keeper_fromDB == undefined || _.isEmpty(vendor_store_keeper_fromDB)) {
      return F.returnMsg('400', '该商家还没有创建店长角色或店长角色无效', 3);
    }
    vendor_store_keeper_fromDB=vendor_store_keeper_fromDB[0].rid;
  }
  if (vendor_store_keeper_fromDB == 0) {
    return F.returnMsg(300, '你要绑定的机构所属的商家还没有创建任何角色，请先创建角色', 1);
  }

  //4.检查用户是否有效
  let userInfo = yield MUsers.getUser('uid,groupid', 'uid=?', [uid]);
  if (userInfo.length == 0 || userInfo[0].groupid == 5) { // gid=5 是禁止访问组
    return F.returnMsg(300, '该用户无效，请检查', 1);
  }
  //5.检查用户是否为该机构商家下的店长，并将用户在角色用户表里面的id提取出来
  let valid_user = false;
  let user_role_id = 0;
  let userRole = yield o2oServiceApi.post('authority', 'userDetail', 1, {act: 1, uid: uid});

  if (userRole && userRole['result'] && userRole['result']['rid'] == vendor_store_keeper_fromDB && userRole['result']['status'] == 1 && userRole['result']['vid'] == vendorId) {
    valid_user = true;
    user_role_id = userRole['result']['id'];
  }
  if (!valid_user) {
    return F.returnMsg(300, '该用户不是有效的店长，请检查店长设置', 1);
  } else if (valid_user && userRole['result']['sid'] && userRole['result']['sid'] == sid) {
    return F.returnMsg(300, '该用户已经绑定为当前店店长', 1);
  }else if(valid_user && userRole['result']['sid']>0 && userRole['result']['sid']!=sid){
    return F.returnMsg(300, '该用户已经绑定为同一家商家下其他分店的店长', 1);
  }
  //6.判断当前机构是否已经绑定到其他店长，如果已绑定，将其关系设为无效
  let organization_role_list = yield o2oServiceApi.post('authority', 'userList', 1, {act: 3, queryObj: {sid: sid}});
  if(organization_role_list.status == 200 && organization_role_list['result'] && organization_role_list['result'].length>0 ) {//存在则将其无效化
    let role_record_id = organization_role_list['result'][0]['id'];
    let organization_role_update = {act: 1, id: role_record_id, sid: 0, status: 1};
    yield o2oServiceApi.post('authority', 'userUpdate', 1, organization_role_update);
  }
  //7.更新用户角色表里面的sid
  let user_organization_role = {act: 1, id: user_role_id, sid: sid};
  let result_user_organization_role = yield o2oServiceApi.post('authority', 'userUpdate', 1, user_organization_role);
  if (parseInt(result_user_organization_role['result']['error']) == 1 || result_user_organization_role.status == 400) {
    return F.returnMsg(300, result_user_organization_role['message']['msg'], 1);
  } else {
    return F.returnMsg(200, '绑定成功', 1,result_user_organization_role['result']);
  }
};